using System;
using Finance.Domain;

namespace Finance.Common
{
    /// <summary>
    /// 产品状态辅助类
    /// </summary>
    public static class ProductStatusHelper
    {
        /// <summary>
        /// 产品状态枚举
        /// </summary>
        public enum ProductStatus
        {
            /// <summary>
            /// 未入库
            /// </summary>
            NotInStock = 0,
            
            /// <summary>
            /// 已入库
            /// </summary>
            InStock = 1,
            
            /// <summary>
            /// 已出库
            /// </summary>
            OutOfStock = 2
        }

        /// <summary>
        /// 获取状态文本
        /// </summary>
        /// <param name="status">状态值</param>
        /// <returns>状态文本</returns>
        public static string GetStatusText(int status)
        {
            return status switch
            {
                0 => "未入库",
                1 => "已入库",
                2 => "已出库",
                _ => "未知状态"
            };
        }

        /// <summary>
        /// 获取状态标签类型
        /// </summary>
        /// <param name="status">状态值</param>
        /// <returns>标签类型</returns>
        public static string GetStatusTagType(int status)
        {
            return status switch
            {
                0 => "info",
                1 => "success",
                2 => "danger",
                _ => "info"
            };
        }

        /// <summary>
        /// 根据库存数量判断状态
        /// </summary>
        /// <param name="productNum">库存数量</param>
        /// <returns>状态值</returns>
        public static int GetStatusByStock(int productNum)
        {
            return productNum > 0 ? (int)ProductStatus.InStock : (int)ProductStatus.NotInStock;
        }

        /// <summary>
        /// 验证库存操作
        /// </summary>
        /// <param name="currentStock">当前库存</param>
        /// <param name="operationAmount">操作数量</param>
        /// <param name="isInbound">是否为入库操作</param>
        /// <returns>验证结果</returns>
        public static StockValidationResult ValidateStockOperation(int currentStock, int operationAmount, bool isInbound)
        {
            if (operationAmount <= 0)
            {
                return new StockValidationResult
                {
                    IsValid = false,
                    Message = "操作数量必须大于0"
                };
            }

            if (isInbound)
            {
                // 入库操作：检查是否会超出库存上限（假设最大库存为999999）
                if (currentStock + operationAmount > 999999)
                {
                    return new StockValidationResult
                    {
                        IsValid = false,
                        Message = "入库后库存数量超出系统上限"
                    };
                }
            }
            else
            {
                // 出库操作：检查库存是否足够
                if (currentStock < operationAmount)
                {
                    return new StockValidationResult
                    {
                        IsValid = false,
                        Message = "库存不足，无法完成出库操作"
                    };
                }
            }

            return new StockValidationResult
            {
                IsValid = true,
                Message = "验证通过"
            };
        }

        /// <summary>
        /// 判断是否需要更新库位
        /// </summary>
        /// <param name="currentStock">当前库存</param>
        /// <param name="operationAmount">操作数量</param>
        /// <param name="currentLocation">当前库位</param>
        /// <param name="newLocation">新库位</param>
        /// <returns>是否需要更新库位</returns>
        public static bool ShouldUpdateLocation(int currentStock, int operationAmount, string currentLocation, string newLocation)
        {
            // 如果当前库位为空或与新库位不同，则需要更新
            if (string.IsNullOrEmpty(currentLocation) || currentLocation != newLocation)
            {
                return true;
            }

            // 如果是入库操作且当前库存为0，则更新库位
            if (currentStock == 0 && operationAmount > 0)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// 更新产品状态
        /// </summary>
        /// <param name="currentStock">当前库存</param>
        /// <param name="operationAmount">操作数量</param>
        /// <param name="isInbound">是否为入库操作</param>
        /// <returns>新的状态值</returns>
        public static int UpdateProductStatus(int currentStock, int operationAmount, bool isInbound)
        {
            if (isInbound)
            {
                // 入库操作
                var newStock = currentStock + operationAmount;
                
                // 如果库存从0变为大于0，则状态变为已入库
                if (currentStock == 0 && newStock > 0)
                {
                    return (int)ProductStatus.InStock;
                }
                // 如果库存已经大于0，保持已入库状态
                else if (newStock > 0)
                {
                    return (int)ProductStatus.InStock;
                }
            }
            else
            {
                // 出库操作
                var newStock = currentStock - operationAmount;
                
                // 如果库存变为0或负数，则状态变为已出库
                if (newStock <= 0)
                {
                    return (int)ProductStatus.OutOfStock;
                }
                // 如果库存仍然大于0，保持已入库状态
                else if (newStock > 0)
                {
                    return (int)ProductStatus.InStock;
                }
            }

            return (int)ProductStatus.NotInStock;
        }

        /// <summary>
        /// 更新产品剩余容量
        /// </summary>
        /// <param name="productAllNum">总容量</param>
        /// <param name="productNum">当前库存数量</param>
        /// <returns>剩余容量</returns>
        public static int UpdateRemainingCapacity(int productAllNum, int productNum)
        {
            return Math.Max(0, productAllNum - productNum);
        }

        /// <summary>
        /// 验证并更新产品信息
        /// </summary>
        /// <param name="product">产品信息</param>
        /// <param name="operationAmount">操作数量</param>
        /// <param name="isInbound">是否为入库操作</param>
        /// <param name="newLocation">新库位（可选）</param>
        /// <returns>更新结果</returns>
        public static ProductUpdateResult UpdateProductInfo(ProductModel product, int operationAmount, bool isInbound, string newLocation = null)
        {
            var result = new ProductUpdateResult();

            try
            {
                // 验证操作
                var validation = ValidateStockOperation(product.ProductNum, operationAmount, isInbound);
                if (!validation.IsValid)
                {
                    result.IsSuccess = false;
                    result.Message = validation.Message;
                    return result;
                }

                // 记录原始库存
                var originalStock = product.ProductNum;

                // 更新库存数量
                if (isInbound)
                {
                    product.ProductNum += operationAmount;
                }
                else
                {
                    product.ProductNum -= operationAmount;
                }

                // 更新产品状态
                product.ProductStatus = UpdateProductStatus(originalStock, operationAmount, isInbound);

                // 更新剩余容量
                product.ProductRemainingNum = UpdateRemainingCapacity(product.ProductAllNum, product.ProductNum);

                // 更新库位（如果需要）
                if (!string.IsNullOrEmpty(newLocation) && ShouldUpdateLocation(originalStock, operationAmount, product.Location, newLocation))
                {
                    product.Location = newLocation;
                }

                // 更新时间和操作人
                product.UpdateTime = DateTime.Now;
                product.UpdateName = "管理员";

                result.IsSuccess = true;
                result.Message = "产品信息更新成功";
                result.UpdatedProduct = product;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = $"产品信息更新失败: {ex.Message}";
            }

            return result;
        }
    }

    /// <summary>
    /// 库存验证结果
    /// </summary>
    public class StockValidationResult
    {
        /// <summary>
        /// 是否验证通过
        /// </summary>
        public bool IsValid { get; set; }

        /// <summary>
        /// 验证消息
        /// </summary>
        public string Message { get; set; }
    }

    /// <summary>
    /// 产品更新结果
    /// </summary>
    public class ProductUpdateResult
    {
        /// <summary>
        /// 是否更新成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 更新消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 更新后的产品信息
        /// </summary>
        public ProductModel UpdatedProduct { get; set; }
    }
} 